﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Bingo.SSQ
{
    public partial class ucForeCast : UserControl
    {
        #region Attributes

        private delegate string myDeleGategetComboBoxSelectedValue();
        private delegate void myDeleGateshowResult(Label label, string num);

        #endregion

        #region ucForeCast

        public ucForeCast()
        {
            InitializeComponent();
        }

        private void ucForeCast_SizeChanged(object sender, EventArgs e)
        {
            this.picClose.Location = new Point(this.Width - this.picClose.Width - 10, 3);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
        }

        #endregion

        #region Event

        #region Button

        private void btnStart_Click(object sender, EventArgs e)
        {
            ((Bingo)this.ParentForm).myThread = new Thread(new ThreadStart(ThreadForeCast));
            ((Bingo)this.ParentForm).myThread.Start();
        }

        #endregion

        #region PictureBox

        private void picClose_MouseEnter(object sender, EventArgs e)
        {
            this.picClose.Image = Properties.Resources.close01;
        }

        private void picClose_MouseLeave(object sender, EventArgs e)
        {
            this.picClose.Image = Properties.Resources.close02;
        }

        private void picClose_Click(object sender, EventArgs e)
        {
            if (((Bingo)this.ParentForm).myThread != null && ((Bingo)this.ParentForm).myThread.IsAlive)
            {
                if (MessageBox.Show("正在进行操作，确定要退出吗？", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                {
                    return;
                }
                ((Bingo)this.ParentForm).myThread.Abort();
            }
            this.Dispose();            
        }

        #endregion

        #endregion

        #region Thread

        private void ThreadForeCast()
        {
            showRuleOne();
            showRuleTwo();
            showRuleThree();
            showRuleFour();
            showRuleFive();
            showRuleSix();
        }

        #endregion

        #region Method

        private void showResult(Label label,string num)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new myDeleGateshowResult(showResult), new object[] { label, num });
            }
            else
            {
                label.Text = num;
            }
        }

        private void showRuleOne()
        {
            DataTable dt = null;
            dt = BingoLib.SSQ.Number.getRedCount("asc", null, null);
            List<string> Reds = new List<string>();
            for (int i = 0; i < 6; i++)
            {
                Reds.Add(dt.Rows[i][0].ToString());
            }
            dt = BingoLib.SSQ.Number.getBlueCount("asc", null, null);
            string Blue = dt.Rows[0][0].ToString();

            Reds = sort(Reds);
            showResult(this.lblR11, Reds[0]);
            showResult(this.lblR12, Reds[1]);
            showResult(this.lblR13, Reds[2]);
            showResult(this.lblR14, Reds[3]);
            showResult(this.lblR15, Reds[4]);
            showResult(this.lblR16, Reds[5]);
            showResult(this.lblBlue1, Blue);
        }

        private void showRuleTwo()
        {
            DataTable dt = null;
            dt = BingoLib.SSQ.Number.getRedCount("desc", null, null);
            List<string> Reds = new List<string>();
            for (int i = 0; i < 6; i++)
            {
                Reds.Add(dt.Rows[i][0].ToString());
            }
            dt = BingoLib.SSQ.Number.getBlueCount("desc", null, null);
            string Blue = dt.Rows[0][0].ToString();

            Reds = sort(Reds);
            showResult(this.lblR21, Reds[0]);
            showResult(this.lblR22, Reds[1]);
            showResult(this.lblR23, Reds[2]);
            showResult(this.lblR24, Reds[3]);
            showResult(this.lblR25, Reds[4]);
            showResult(this.lblR26, Reds[5]);
            showResult(this.lblBlue2, Blue);
        }

        private void showRuleThree()
        {
            DataTable dt = null;
            dt = BingoLib.SSQ.Number.getRedCount("desc", null, null);
            List<string> Reds = new List<string>();
            for (int i = 0; i < 3; i++)
            {
                Reds.Add(dt.Rows[i][0].ToString());
            }
            for (int i = 30; i < 33; i++)
            {
                Reds.Add(dt.Rows[i][0].ToString());
            }
            dt = BingoLib.SSQ.Number.getBlueCount("desc", null, null);
            List<string> Blues = new List<string>();
            Blues.Add(dt.Rows[0][0].ToString());
            Blues.Add(dt.Rows[15][0].ToString());

            Reds = sort(Reds);
            showResult(this.lblR31, Reds[0]);
            showResult(this.lblR32, Reds[1]);
            showResult(this.lblR33, Reds[2]);
            showResult(this.lblR34, Reds[3]);
            showResult(this.lblR35, Reds[4]);
            showResult(this.lblR36, Reds[5]);

            Random Ran = new Random();
            showResult(this.lblBlue3, Blues[Ran.Next(0, 2)]);
        }

        private void showRuleFour()
        {
            DataTable dt = null;
            dt = BingoLib.SSQ.Number.getRedCount("desc", null, null);
            List<string> tmpReds = new List<string>();
            List<string> Reds = new List<string>();

            for (int i = 0; i < 6; i++)
            {
                tmpReds.Add(dt.Rows[i][0].ToString());
            }
            for (int i = 27; i < 33; i++)
            {
                tmpReds.Add(dt.Rows[i][0].ToString());
            }
            dt = BingoLib.SSQ.Number.getBlueCount("desc", null, null);
            List<string> Blues = new List<string>();
            Blues.Add(dt.Rows[0][0].ToString());
            Blues.Add(dt.Rows[15][0].ToString());

            Random Ran = new Random();
            for (int i = 0; i < 6; i++)
            {
                int index = Ran.Next(0, 12 - i);
                Reds.Add(tmpReds[index]);
                tmpReds.RemoveAt(index);
            }
            Reds = sort(Reds);

            showResult(this.lblR41, Reds[0]);
            showResult(this.lblR42, Reds[1]);
            showResult(this.lblR43, Reds[2]);
            showResult(this.lblR44, Reds[3]);
            showResult(this.lblR45, Reds[4]);
            showResult(this.lblR46, Reds[5]);

            showResult(this.lblBlue4, Blues[Ran.Next(0, 2)]);
        }

        private void showRuleFive()
        {
            DataTable dt = null;
            dt = BingoLib.SSQ.Number.getRedCount("desc", null, null);
            List<string> tmpReds = new List<string>();
            List<string> Reds = new List<string>();

            for (int i = 0; i < 6; i++)
            {
                tmpReds.Add(dt.Rows[i][0].ToString());
            }
            for (int i = 13; i < 20; i++)
            {
                tmpReds.Add(dt.Rows[i][0].ToString());
            }
            for (int i = 27; i < 33; i++)
            {
                tmpReds.Add(dt.Rows[i][0].ToString());
            }
            dt = BingoLib.SSQ.Number.getBlueCount("desc", null, null);
            List<string> Blues = new List<string>();
            Blues.Add(dt.Rows[0][0].ToString());
            Blues.Add(dt.Rows[7][0].ToString());
            Blues.Add(dt.Rows[8][0].ToString());
            Blues.Add(dt.Rows[15][0].ToString());

            Random Ran = new Random();
            for (int i = 0; i < 6; i++)
            {
                int index = Ran.Next(0, 19 - i);
                Reds.Add(tmpReds[index]);
                tmpReds.RemoveAt(index);
            }
            Reds = sort(Reds);

            showResult(this.lblR51, Reds[0]);
            showResult(this.lblR52, Reds[1]);
            showResult(this.lblR53, Reds[2]);
            showResult(this.lblR54, Reds[3]);
            showResult(this.lblR55, Reds[4]);
            showResult(this.lblR56, Reds[5]);

            showResult(this.lblBlue5, Blues[Ran.Next(0, 4)]);
        }

        private void showRuleSix()
        {
            DataTable dt = null;
            dt = BingoLib.SSQ.Number.getRedCount("desc", null, null);
            List<string> tmpReds = new List<string>(new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33" });
            List<string> tmpBlues = new List<string>(new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16" });
            List<string> Reds = new List<string>();

            Random Ran=new Random();
            for (int i = 0; i < 6; i++)
            {
                int index = Ran.Next(0, 33 - i);
                Reds.Add(tmpReds[index]);
                tmpReds.RemoveAt(index);
            }
            Reds = sort(Reds);

            showResult(this.lblR61, Reds[0]);
            showResult(this.lblR62, Reds[1]);
            showResult(this.lblR63, Reds[2]);
            showResult(this.lblR64, Reds[3]);
            showResult(this.lblR65, Reds[4]);
            showResult(this.lblR66, Reds[5]);
            showResult(this.lblBlue6, tmpBlues[Ran.Next(0, 16)]);
        }

        private List<string> sort(List<string> Numbers)
        {
            List<string> rval = new List<string>();
            foreach (string num in Numbers)
            {
                if (rval.Count == 0)
                {
                    rval.Add(num);
                    continue;
                }
                for (int i = 0; i < rval.Count; i++)
                {
                    if (int.Parse(num) < int.Parse(rval[i]))
                    {
                        rval.Insert(i, num);
                        break;
                    }
                }
                if (int.Parse(num) > int.Parse(rval[rval.Count - 1])) rval.Add(num);
            }

            return rval;
        }

        #endregion
    }
}
